/**
 * Created By wifi_uncle on 2019/11/6
 */

import API from '../api'
import Utils from '../utils'
import Config from '../config'
import request from "../request";
export default {
  /**
   * 获取推荐人列表
   * @param premisesId
   * @param _this
   * @returns {PromiseLike<any> | Promise<any> | *}
   */
  getAdviserList (premisesId, _this) {
    let url = Utils.getPrefix(_this) + API.ADVISER.ADVISER_LIST
    console.log("url = " + url);
    let body = {}
    body.params = {
      // premisesId: premisesId,
      id: premisesId
    }
    return _this.$http.post(url, body).then(rsp => { // 无同步
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        return Promise.resolve(data.resData.list)
      } else {
        Utils.showFailMsg('获置业顾问失败')
        return Promise.reject(data)
      }
    }, e => {
      Utils.showFailMsg('获置业顾问失败')
      return Promise.reject(e)
    })
  },
  saveAdviser ({premisesId = '', adviserList = [], _this, type = 'ADD'}) {
    let url = type === 'ADD' ? API.ADVISER.SAVE_ADVISER : API.ADVISER.DEL_ADVISER
    url = Utils.getPrefix(_this) + url
    let body = {}
    body.params = {
      premisesId,
      list: adviserList
    }
    return _this.$http.post(url, body).then(rsp => {
      const data = rsp.data
      if (data && data.result === Config.SUCCESS_CODE) {
        Utils.showSuccessMsg('操作成功')
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('操作失败')
        return Promise.reject(data)
      }
    }).catch(e => {
      Utils.Log(e)
      Utils.Log('操作失败')
      return Promise.reject(e)
    })
  },
  getCPCountByAdviser ({adviserUuid, _this}) {
    let url = Utils.getPrefix(_this) + API.ADVISER.ADVISER_CPCOUNT
    let body = {}
    body.params = {
      adviserUuid
    }
    return _this.$http.post(url, body).then(rsp => { // 无同步
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('获取置业顾问失败')
        return Promise.reject(data)
      }
    }, e => {
      Utils.showFailMsg('获取置业顾问失败')
      return Promise.reject(e)
    })
  },
  changeCPAdviser ({premisesId,customerPremisesId,oldAdviser, newAdviser, _this}) {
    let url = Utils.getPrefix(_this) + API.ADVISER.ADVISER_CHANGE
    let body = {}
    body.params = {
      premisesId,
      customerPremisesId,
      oldAdviser,
      newAdviser
    }
    return _this.$http.post(url, body).then(rsp => { // 无同步
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        Utils.showSuccessMsg('变更置业顾问成功')
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('变更置业顾问失败')
        return Promise.reject(data)
      }
    }, e => {
      Utils.showFailMsg('变更置业顾问失败')
      return Promise.reject(e)
    })
  },
  delAdviser ({id, _this}) {
    let url = Utils.getPrefix(_this) + API.ADVISER.DEL_ADVISER
    let body = {}
    body.params = {
      id
    }
    return _this.$http.post(url, body).then(rsp => { // 无同步
      const data = rsp.body
      if (data && data.result === Config.SUCCESS_CODE) {
        return Promise.resolve(data)
      } else {
        Utils.showFailMsg('删除置业顾问失败')
        return Promise.reject(data)
      }
    }, e => {
      Utils.showFailMsg('删除置业业顾问失败')
      return Promise.reject(e)
    })
  },
}
